package com.yozinga;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MerchantLocationMap extends FragmentActivity {
	GoogleMap googleMap;
	MarkerOptions markerOptions;
	LatLng latLng;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_merchantlocation_map);
		SupportMapFragment supportMapFragment = (SupportMapFragment)
				getSupportFragmentManager().findFragmentById(R.id.map);

		Intent intent = this.getIntent();
		
		// Getting a reference to the map
		googleMap = supportMapFragment.getMap();

		// Setting a click event handler for the map
		latLng = new LatLng(Double.parseDouble(intent.getStringExtra("latitude")),Double.parseDouble(intent.getStringExtra("longitude")));
		//latLng = new LatLng(17.4406, 78.3911);

		googleMap.clear();

		// Animating to the touched position
		googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
		
		googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
		
		// Creating a marker
		markerOptions = new MarkerOptions();


		// Setting the position for the marker
		markerOptions.position(latLng);

		// Placing a marker on the touched position
		googleMap.addMarker(markerOptions);

		// Adding Marker on the touched location with address
		new ReverseGeocodingTask(getBaseContext()).execute(latLng);

	}

	private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String>{
		Context mContext;

		public ReverseGeocodingTask(Context context){
			super();
			mContext = context;
		}

		// Finding address using reverse geocoding
		@Override
		protected String doInBackground(LatLng... params) {
			Geocoder geocoder = new Geocoder(mContext);
			double latitude = params[0].latitude;
			double longitude = params[0].longitude;

			List<Address> addresses = null;
			String addressText="";

			try {
				addresses = geocoder.getFromLocation(latitude, longitude,1);
			} catch (IOException e) {
				e.printStackTrace();
			}

			if(addresses != null && addresses.size() > 0 ){
				Address address = addresses.get(0);

				addressText = String.format("%s, %s, %s",
						address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
								address.getLocality(),
								address.getCountryName());
			}

			return addressText;
		}

		@Override
		protected void onPostExecute(String addressText) {
			// Setting the title for the marker.
			// This will be displayed on taping the marker
			markerOptions.title(addressText);
Log.i("Today","Marker Address :"+addressText);
			// Placing a marker on the touched position
			googleMap.addMarker(markerOptions);

		}
	}
}
